/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eng.scc.core.parts;

import com.eng.scc.core.data.DeviceInfo;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;

/**
 *
 * @author Rafael
 */
public class DeviceInfoTable extends JTable
{
    private static final int COL_DATE    = 0;
    private static final int COL_INFO     = 1;
    
    private static final String[] COLUMN_NAMES = 
    {
        "Data/Hora",
        "Valor (ml)"
    };

    @Override
    protected JTableHeader createDefaultTableHeader()
    {
        return new JTableHeader( columnModel )
        {
            @Override
            public String getToolTipText( MouseEvent e )
            {
                java.awt.Point p = e.getPoint();
                int index = columnModel.getColumnIndexAtX( p.x );
                int realIndex = columnModel.getColumn( index ).getModelIndex();
                return COLUMN_NAMES[realIndex];
            }
        };
    }
    
    private static class DeviceInfoTableModel extends AbstractTableModel
    {
        private SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy - HH:mm" );
        
        private List<DeviceInfo> deviceInfos = new ArrayList<DeviceInfo>();
        
        public DeviceInfoTableModel()
        {
        }

        @Override
        public String getColumnName( int column )
        {
            return COLUMN_NAMES[column];
        }
        
        @Override
        public int getRowCount()
        {
            return deviceInfos.size();
        }

        @Override
        public int getColumnCount()
        {
            return COLUMN_NAMES.length;
        }

        @Override
        public Object getValueAt( int rowIndex, int columnIndex )
        {
            DeviceInfo v = deviceInfos.get( rowIndex );
            
            switch ( columnIndex )
            {
                case COL_DATE:
                    return dateFormat.format( v.getTransmitionDate() );
                case COL_INFO:
                    return v.getInfo();
            }
            
            return null;
        }

        @Override
        public boolean isCellEditable( int rowIndex, int columnIndex )
        {
            return false;
        }

        public void setDevicesInfos( List<DeviceInfo> deviceInfos )
        {
            this.deviceInfos = deviceInfos;
            fireTableDataChanged();
        }

        public List<DeviceInfo> getDeviceInfos()
        {
            return deviceInfos;
        }
    }
    
    private DeviceInfoTableModel tableModel = new DeviceInfoTableModel();
    
    public DeviceInfoTable()
    {
        setModel( tableModel );
//        setRootVisible( false );
//        setShowsRootHandles( true );
        setRowHeight( 25 );
    }
    
    public void setDeviceInfos( List<DeviceInfo> deviceInfos )
    {
        tableModel.setDevicesInfos( deviceInfos );
    }
    
    public List<DeviceInfo> getDeviceInfos()
    {
        return tableModel.getDeviceInfos();
    }
    
    public DeviceInfo getSelectedValue()
    {
        if ( getSelectedRow() >= 0 )
        {
             return tableModel.getDeviceInfos().get( getSelectedRow() );
        }
        
        return null;
    }
}
